import logging

import requests

from config import BASE_URL


class IHRMEmpManageApi:

    @classmethod
    def get_token(cls):
        url = BASE_URL + '/api/sys/login'
        header = {"Content-Type": "application/json"}
        json = {
            "mobile": "13800000002",
            "password": "123456"
        }
        r = requests.post(url, headers=header, json=json)
        token = r.json().get('data')

        return token

    @classmethod
    def add_emp(cls, add_emp_data) -> requests.Response:
        url = BASE_URL + '/api/sys/user'
        header = {
            "Content-Type": "application/json",
            "Authorization": cls.get_token()
        }
        logging.info(f"请求数据: {add_emp_data}")
        r = requests.post(url, headers=header, json=add_emp_data)

        return r

    @classmethod
    def del_emp(cls, emp_id) -> requests.Response:
        url = BASE_URL + f'/api/sys/user/{emp_id}'
        header = {
            "Authorization": cls.get_token()
        }
        r = requests.delete(url, headers=header)

        return r

    @classmethod
    def change_emp(cls, emp_id, change_emp_data) -> requests.Response:
        url = BASE_URL + f'/api/sys/user/{emp_id}'
        header = {
            "Content-Type": "application/json",
            "Authorization": cls.get_token()
        }
        r = requests.put(url, headers=header, json=change_emp_data)

        return r

    @classmethod
    def get_emp(cls, emp_id):
        url = BASE_URL + f'/api/sys/user/{emp_id}'
        header = {
            "Authorization": cls.get_token()
        }
        r = requests.get(url, headers=header)

        return r


if __name__ == '__main__':
    # r = IHRMEmpManageApi.get_token()

    # 添加
    # data = {
    #     "username": "张三",
    #     "mobile": "16600000010",
    #     "workNumber": "00001"
    # }
    # d = IHRMEmpManageApi.add_emp(data)
    # print(d.status_code)
    # print(d.json())

    # 删除
    # a = IHRMEmpManageApi.del_emp('1571704077027389440')
    # print(a.status_code)
    # print(a.json())

    # 修改
    # id = '1571704077027389440'
    # data = {
    #     "username": "张三_new",
    #     "password": "999999",
    #     "departmentId": "100001"
    # }
    #
    # b = IHRMEmpManageApi.change_emp(id, data)
    # print(b.status_code)
    # print(b.json())

    # 查询
    id = '1571704077027389440'
    c = IHRMEmpManageApi.get_emp(id)
    print(c.status_code)
    print(c.json())
